package com.link.base.base.mpconfig.controller;
/*
 * createdby: 闫有霞
 * created: 2018-11-19
 * comments:
 */

import com.link.base.base.mpconfig.model.Mp;
import com.link.base.base.mpconfig.model.MpInnerMenu;
import com.link.base.base.mpconfig.service.MpInnerMenuService;
import com.link.base.base.mpconfig.service.MpService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小程序配置
 *
 * @author: 闫有霞
 * @date: 2019/12/19
 */
@Controller
@RequestMapping("/link/mp")
public class MpController extends BasicController<Mp> {

    @Resource
    private MpService mpService;

    @Resource
    private MpInnerMenuService mpInnerMenuService;

    @Override
    public BasicService<Mp> getBasicService() throws Exception {
        return mpService;
    }

    /**
     * 根据条件查询记录,默认分页(若将'pageFlag'设置为'false'，则不分页)
     */
    @RequestMapping(value = "/queryMpInfoByEncryptionId")
    @ResponseBody
    public Map<String, Object> queryMpInfoByEncryptionId(@RequestBody Mp entity, HttpSession session,
                                                         HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            entity = mpService.queryMpInfoByEncryptionId(entity);
            result.put("success", true);
            result.put("result", entity);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
    /**
     *@createdBy 闫有霞
     *@date 2018/11/30
     *@methods: setMain
     *@param: Mp entity
     *@return:
     *@description: 设为主要的
     **/

    @RequestMapping(value = "/setMain")
    @ResponseBody
    public Map<String, Object> setMain(@RequestBody Mp mp, HttpSession session,
                                                         HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            mpService.setMainFlag(mp);
            result.put("success", true);
            result.put("result", mp);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     *@createdBy 闫有霞
     *@date 2018/11/30
     *@methods: updateValid
     *@param: Mp entity
     *@return:
     *@description: 更新状态
     **/

    @RequestMapping(value = "/updateValid")
    @ResponseBody
    public Map<String, Object> updateValid(@RequestBody Mp mp, HttpSession session,
                                       HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            mpService.updateMainFlag(mp);
            result.put("success", true);
            result.put("result", mp);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     *@createdBy 闫有霞
     *@date 2018/11/30
     *@methods: insertMp
     *@param: Mp entity
     *@return:
     *@description: 添加操作
     **/

    @RequestMapping(value = "/insertMp")
    @ResponseBody
    public Map<String, Object> insertMp(@RequestBody Mp mp, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            Mp main = new Mp();
            main.setMainFlag("Y");
            List<Mp>  mainList = new ArrayList<>();
            /*查询主要的记录*/
            mainList = mpService.queryAll(main);
            /*如果数据库主要记录为0 -》设置当前要添加的记录为主要记录
            * 主要记录 必定是有效的 -》设置当前要添加的记录为有效的*/
            if (mainList.size() == 0) {
                mp.setMainFlag("Y");
                mp.setValidFlag("Y");
            } else {
                /*如果数据库主要记录不为0（已经有主要记录） -》设置当前要
                *添加的记录为不是主要记录
                */
                mp.setMainFlag("N");
                mp.setValidFlag("Y");
            }
            mp.setLeaveInfoFlag("N");
            mpService.insert(mp);
            if (mp.getMpMenuList() != null && mp.getMpMenuList().size() > 0) {
                for (String item : mp.getMpMenuList()) {
                    MpInnerMenu mpMenu = new MpInnerMenu();
                    mpMenu.setMpId(mp.getId());
                    mpMenu.setMpMenuId(Long.parseLong(item));
                    mpInnerMenuService.insert(mpMenu);
                }
            }

            result.put("success", true);
            result.put("result", mp);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
    /**
     *@createdBy 闫有霞
     *@date 2018/11/30
     *@methods: updateMp
     *@param: Mp entity
     *@return:
     *@description: 编辑操作
     **/

    @RequestMapping(value = "/updateMp")
    @ResponseBody
    public Map<String, Object> updateMp(@RequestBody Mp mp, HttpSession session,
                                        HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            mpService.update(mp);
            if (mp.getMpMenuList() != null ) {
                MpInnerMenu mpMenu = new MpInnerMenu();
                mpMenu.setMpId(mp.getId());
                mpInnerMenuService.deleteByMpId(mpMenu);
                for (String item : mp.getMpMenuList()) {
                    MpInnerMenu subMenu = new MpInnerMenu();
                    subMenu.setMpId(mp.getId());
                    subMenu.setMpMenuId(Long.parseLong(item));
                    mpInnerMenuService.insert(subMenu);
                }
            }

            result.put("success", true);
            result.put("result", mp);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     *@createdBy 闫有霞
     *@date 2018/11/30
     *@methods: deleteMp
     *@param: Mp entity
     *@return:
     *@description: 删除操作
     **/

    @RequestMapping(value = "/deleteMp")
    @ResponseBody
    public Map<String, Object> deleteMp(@RequestBody Mp mp, HttpSession session,
                                         HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            mpService.deleteById(mp);
            MpInnerMenu mpMenu = new MpInnerMenu();
            mpMenu.setMpId(mp.getId());
            mpInnerMenuService.deleteByMpId(mpMenu);
            result.put("success", true);
            result.put("result", mp);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 查询当前小程序对应的客服电话
     * @author KuangRuifeng
     * @date 2018年12月14日
     */
    @RequestMapping(value = "/queryCostomPhone")
    @ResponseBody
    public Map<String, Object> queryCostomPhone() {
        Map<String, Object> result = new HashMap<>(4);
        try {
            String costomPhone = mpService.queryCostomPhone();
            result.put("success", true);
            result.put("result", costomPhone);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     *@createdBy 闫有霞
     *@date 2018/12/20
     *@methods: getMpMenu
     *@param: Mp entity
     *@return: menuInnerMenu
     *@description: 获取小程序下的菜单
     **/
    @RequestMapping(value = "/getMpMenu")
    @ResponseBody
    public Map<String, Object> getMpMenu(@RequestBody Mp entity, HttpSession session,
                                        HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<MpInnerMenu> checkMenuList =  mpService.queryMenuByMpId(entity.getId());
            result.put("success", true);
            result.put("result", checkMenuList);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     *@createdBy 闫有霞
     *@date 2018/11/30
     *@methods: updateLeaveInfoFlag
     *@param: Mp entity
     *@return:
     *@description: 更新是否强制留资状态
     **/

    @RequestMapping(value = "/updateLeaveInfoFlag")
    @ResponseBody
    public Map<String, Object> updateLeaveInfoFlag(@RequestBody Mp mp, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            mpService.updateLeaveInfoFlag(mp);
            result.put("success", true);
            result.put("result", mp);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     *@createdBy 闫有霞
     *@date 2018/11/30
     *@methods: updateShareFriendFlag
     *@param: Mp entity
     *@return:
     *@description: 是否分享朋友圈
     **/

    @RequestMapping(value = "/updateShareFriendFlag")
    @ResponseBody
    public Map<String, Object> updateShareFriendFlag(@RequestBody Mp mp, HttpSession session,
                                                     HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            mpService.updateShareFriendFlag(mp);
            result.put("success", true);
            result.put("result", mp);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }



}
